<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of user_model
 *
 * @author Костя
 */
class User_model extends CI_Model {

    public $user;

    function __construct() {
        parent::__construct();
    }

    function getUser($uid) {
        if (is_array($this->user)) {
            return $this->user;
        }

        $query = $this->db->get_where('users', array('id' => $uid));
        if ($query->num_rows() != 1)
            return FALSE;
        else {
            $data = $query->result();
            $this->user = $data;
            return $data;
        }
    }

    function make_login($user, $password) {

        $query = $this->db->get_where('users', array('login' => $user, 'password' => $password));
        if ($query->num_rows() == 1) {
            $result = $query->result();
            $newdata = array(
                'user' => $result->id,
                'logged_in' => TRUE
            );

            $this->session->set_userdata($newdata);
            return true;
        } else {
            return false;
        }
    }

    function user_register($login, $password, $mail, $cityid, $phone, $sing_in = FALSE) {
        $password = md5($password);
        $data = array(
            'login' => $login,
            'password' => $password,
            'mail' => $mail,
            'phone' => $phone,
            'city' => $cityid,
        );

        $result = $this->db->insert('users', $data);
        if ($sing_in) {
            if ($this->make_login($login, $password)) {
                return true;
            } else {
                return false;
            }
        } else {
            return $this->db->insert_id();
        }
    }

    function user_subscribe($uid, $mail) {
        if ($uid) {
            $query = $this->db->get_where('users', array('id' => $uid));
            $result = $query->result();
            $mail = $result->mail;
            $this->db->delete('subscribe', array('mail' => $mail));
        } elseif ($mail) {
            
        }
    }

    function user_unsubscribe($uid, $mail = FALSE, $hash = FALSE) {
        if ($uid && $mail === FALSE) {
            $query = $this->db->get_where('users', array('id' => $uid));
            $result = $query->result();
            $mail = $result->mail;
            return $this->db->delete('subscribe', array('mail' => $mail));
        } elseif ($mail && $hash) {
            return $this->db->delete('subscribe', array('mail' => $mail, 'hash' => $hash));
        }
    }

    function user_edit($uid, $data) {
        $data = array(
            'title' => $title,
            'name' => $name,
            'date' => $date
        );

        $this->db->where('id', $uid);
        return $this->db->update('users', $data);
    }

    function user_getOrders($uid,$filter_date = Array()) {
        $this->db->where('date >',$filter_date[0]);
        $this->db->where('date <',$filter_date[1]);
        $query = $this->db->get('orders');
        return $query->result();
    }

    function user_getCoupons() {
        
    }

}

?>
